package Friend;

import Population.*;
import World.*;

public class FriendArray {
	Inhabitant[] friends;
	int[] friendRepeat;
	public FriendArray()
	{
		friends = new Inhabitant[0];
		friendRepeat = new int[0];
	}
	
	public int getLength()
	{
		return friends.length;
	}
	
	public int[] GetFriendRepeat()
	{
		return friendRepeat;
	}
	
	/**
	 * Tested
	 * @param size
	 */
	public void ReallocationInhabArray(int size)
	{
		Inhabitant[] tempFriends = new Inhabitant[size];
		int[] tempFriendRepeat = new int[size];
		if(friends.length < size)
		{
			if(World.PrintToOutFile)
				World.outFile.println("Realloc-larger:"+size);
			for (int i =0; i<friends.length; i++)
			{
				tempFriends[i] = friends[i];
				tempFriendRepeat[i] = friendRepeat[i];
			}
			friends = new Inhabitant[size];
			friendRepeat = new int[size];
			for (int i =0; i<friends.length; i++)
			{
				friends[i] = tempFriends[i];
				friendRepeat[i] = tempFriendRepeat[i];
			}
		}
		else if ( size < friends.length)
		{
			if(World.PrintToOutFile)
				World.outFile.println("Realloc-smaller:"+size);
			if(size == 0)
			{
				friends = new Inhabitant[size];
				friendRepeat = new int[size];
			}
			else
			{
				int count = 0;
				for (int i =0; i<friends.length; i++)
				{
					//if(friends[i].isAlive)
					//{
					if(World.PrintToOutFile)
						World.outFile.println("Realloc-smaller:: friend included"+friends[i].getID());
					Inhabitant temp = friends[i];
					tempFriends[count] = temp;
					tempFriendRepeat[count] = friendRepeat[i];
					count++;
					//}
					//else
					//{
						//World.outFile.println("Realloc-smaller:: friend not included"+friends[i].getID());
					//}
					if(count == size)
						break;
				}
				friends = new Inhabitant[size];
				friendRepeat = new int[size];
				for (int i =0; i<friends.length; i++)
				{
					friends[i] = tempFriends[i];
					friendRepeat[i] = tempFriendRepeat[i];
				}
			}
		}
	}
	
	/**
	 * Tested
	 * @param friendID
	 */
	public void AddFriend(Inhabitant me, Inhabitant friendID)
	{
		
		if(friendID.getID() != me.getID())
		{
			ReallocationInhabArray(friends.length+1);
			friends[friends.length-1] = friendID; 
			friendRepeat[friends.length-1] = 0;
		}
	}
	
	public void AddFriendFORCE(Inhabitant friendID)
	{
		if(World.PrintToOutFile)
			World.outFile.println("Adding Friend Reallocation:"+(friends.length+1));
	
		ReallocationInhabArray(friends.length+1);
		friends[friends.length-1] = friendID; 
		friendRepeat[friends.length-1] = 0;
		
		if(World.PrintToOutFile)
			World.outFile.println("Add Friend: " +ReportFriends() +  "Size:"+this.getLength());
		
	}
	
	/**
	 * Tested - removes the friend from the list
	 */
	public void RemoveFriend(int friendID)
	{
		int count = 0;
		for(int i=0; i<friends.length; i++)
		{
			if(friends[i]!=null && friends[i].getID() != friendID)
			{
				friends[count] = friends[i];
				friendRepeat[count] = friendRepeat[i];
				count ++;
			}
		}
		if(World.PrintToOutFile)
			World.outFile.println("Removing Friend Reallocation:"+count);
		ReallocationInhabArray(count);
		if(World.PrintToOutFile)
			World.outFile.println("Removed Friend: "+ReportFriends() +  "Size:"+this.getLength());
	}
	
	/**
	 * Tested
	 */
	public Inhabitant[] getFriends()
	{
		return friends;
	}
	
	/**
	 * Testing - Hitting null on occasion - Prevents duplicate friends
	 * @return
	 */
	public boolean findDuplicate(int friendID)
	{
		if(World.PrintToOutFile)
			World.outFile.println("Find Duplicate: "+ReportFriends() +  "Size:"+this.getLength());
		for (int i=0; i<friends.length; i++)
		{
			try{
				if(friends[i].getID() == friendID)
				{
					return true;
				}
			}
			catch(Exception e)
			{
				if(World.PrintToOutFile){
					World.outFile.println("ID:"+friendID);
					World.outFile.println("Length:"+friends.length);
					World.outFile.println("i:"+i);
					World.outFile.println(e.toString());
				}
				System.exit(0);
			}
		}
		return false;
	}
		
	
	public String ReportFriends()
	{
		String s = "";
		try{
			for(int i=0; i<friends.length; i++)
			{
				s = s + friends[i].getID() + "("+friendRepeat[i]+") ";
			}
		}
		catch (Exception e)
		{
			if(World.PrintToOutFile){
				World.outFile.println("Exception Caught:"+e.toString());
				World.outFile.println("Exception Caught:"+e.getStackTrace());
			}
		}
		return s;
	}
	
	public void FriendSpotted(int i)
	{
		friendRepeat[i]++;
	}
}
